# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pygobject

pkgname=("${MINGW_PACKAGE_PREFIX}-python2-gobject" "${MINGW_PACKAGE_PREFIX}-python3-gobject" "${MINGW_PACKAGE_PREFIX}-pygobject-devel")
pkgver=3.12.1
pkgrel=1
pkgdesc="Allows you to load glade interface files in a program at runtime (mingw-w64)"
arch=(any)
url="http://www.pygtk.org"
license=('LGPL')
groups=("${MINGW_PACKAGE_PREFIX}")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-gobject-introspection" "${MINGW_PACKAGE_PREFIX}-gnome-common")
options=('staticlibs' 'strip')
source=(http://ftp.gnome.org/pub/gnome/sources/pygobject/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
    0002-no-undefined-on.mingw.patch
    0003-dll.mingw.patch
    0004-python-m4.patch
    0005-mingw-include-math.patch
    0006-link-with-libffi.patch)
md5sums=('8608682f221feaac81adb3f4e40dbef3'
         '147cb1ce51e82d1912493d17326908ff'
         '2bb8ab70eb2418b524aea04d4d996b91'
         'bb9a4f77ae6f5bdad1b93f9199c31509'
         '554f9eaca27decf1395153b00abe2469'
         '4d215b260d7caa9226d29896d21a93cb')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0002-no-undefined-on.mingw.patch
  patch -p1 -i ${srcdir}/0003-dll.mingw.patch
  patch -p1 -i ${srcdir}/0004-python-m4.patch
  patch -p1 -i ${srcdir}/0005-mingw-include-math.patch
  patch -p1 -i ${srcdir}/0006-link-with-libffi.patch
  autoreconf -fi
  #WANT_AUTOMAKE=latest ./autogen.sh
}

build() {
  cd "$srcdir"

  rm -rf python{2,3}-build
  mkdir python{2,3}-build devel
  CFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  CPPFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  for builddir in python{2,3}-build; do
    #cp -r ${_realname}-$pkgver $builddir
    pushd $builddir
    PYTHON=${MINGW_PREFIX}/bin/${builddir%-build} \
      ../${_realname}-$pkgver/configure --prefix=${MINGW_PREFIX} \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${MINGW_CHOST} \
        --disable-silent-rules
      make VERBOSE=1
    popd
  done
}

package_python2-gobject() {
  pkgdesc="Python 2 bindings for GObject2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2" "${MINGW_PACKAGE_PREFIX}-python2-cairo" "${MINGW_PACKAGE_PREFIX}-libffi" "${MINGW_PACKAGE_PREFIX}-gobject-introspection" "${MINGW_PACKAGE_PREFIX}-pygobject-devel=$pkgver")

  cd python2-build
  make DESTDIR="$pkgdir" install
  mv "${pkgdir}${MINGW_PREFIX}"/{include,lib/pkgconfig} "$srcdir/devel"
}

package_python3-gobject() {
  pkgdesc="Python 3 bindings for GObject2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2" "${MINGW_PACKAGE_PREFIX}-python3-cairo" "${MINGW_PACKAGE_PREFIX}-libffi" "${MINGW_PACKAGE_PREFIX}-gobject-introspection" "${MINGW_PACKAGE_PREFIX}-pygobject-devel=$pkgver")

  cd python3-build
  make DESTDIR="$pkgdir" install
  
  rm -r "${pkgdir}${MINGW_PREFIX}"/{include,lib/pkgconfig}
}

package_pygobject-devel() {
  pkgdesc="Development files for the pygobject bindings"
  cd "devel"
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/{include,lib}
  mv include "${pkgdir}${MINGW_PREFIX}/"
  mv pkgconfig "${pkgdir}${MINGW_PREFIX}/lib/"
}

package_mingw-w64-i686-pygobject-devel() {
  package_pygobject-devel
}

package_mingw-w64-x86_64-pygobject-devel() {
  package_pygobject-devel
}

package_mingw-w64-i686-python2-gobject() {
  package_python2-gobject
}

package_mingw-w64-i686-python3-gobject() {
  package_python3-gobject
}

package_mingw-w64-x86_64-python2-gobject() {
  package_python2-gobject
}

package_mingw-w64-x86_64-python3-gobject() {
  package_python3-gobject
}
